import os
import pathlib
from typing import List
from functools import lru_cache
from pydantic_settings import BaseSettings, SettingsConfigDict



class Settings(BaseSettings):
    # 基本配置
    DESC: str = "RPA API 服务"
    VERSION: str = os.getenv("VERSION", "1.0.0")
    LOG_LEVEL: str = "DEBUG"
    WEB_HOST: str = os.getenv("WEB_HOST", "0.0.0.0")
    WEB_PORT: int = int(os.getenv("WEB_PORT", 5000)) # 默认端口 

    # 项目路径
    PROJECT_PATH: pathlib.Path = pathlib.Path(__file__).parent.parent
    LOG_PATH: pathlib.Path = PROJECT_PATH / "logs"  # 日志路径

    # 静态文件
    STATIC_FILE: bool = True
    STATIC_PATH: pathlib.Path = PROJECT_PATH / "static"

    # MySQL配置
    MYSQL_ENGINE: str = "mysql"
    MYSQL_ECHO: bool = True  # 是否显示SQL语句
    MYSQL_HOST: str = os.getenv("MYSQL_HOST", "127.0.0.1")
    MYSQL_PORT: int = int(os.getenv("MYSQL_PORT", 3306))
    MYSQL_USER: str = os.getenv("MYSQL_USER", "root")
    MYSQL_PASSWORD: str = os.getenv("MYSQL_PASSWORD", "123456")
    MYSQL_DATABASE: str = os.getenv("MYSQL_DATABASE", "fd_plateform")
    MYSQL_ENCODING: str = os.getenv("MYSQL_ENCODING", "utf8mb4")
    MYSQL_ADD_EXCEPTION_HANDLERS: bool = os.getenv("MYSQL_ADD_EXCEPTION_HANDLERS", "True").lower() == "true"

    # ORM
    GENERATE_SCHEMAS: bool = False # 是否生成数据库架构, 生产环境关闭
    
    # Redis配置
    REDIS_HOST: str = os.getenv("REDIS_HOST", "127.0.0.1")
    REDIS_PORT: int = int(os.getenv("REDIS_PORT", 6379))
    REDIS_PASSWORD: str = os.getenv("REDIS_PASSWORD", "123456")
    REDIS_DB: int = int(os.getenv("REDIS_DB", 1))
    REDIS_ENCODING: str = os.getenv("REDIS_ENCODING", "utf-8")
    
    # CORS配置
    CORS_ORIGINS: List[str] = ["*"]  # 允许的跨域请求来源，默认允许所有来源

    # 影刀配置
    YINGDAO_TOKEN_URL:str = "https://api.yingdao.com/oapi/token/v2/token/create"
    ACCESS_KEY_ID:str = "tZnJWDhERpcu2zU7@platform"
    ACCESS_KEY_SECRET:str = "WBDXsAEw23uzRKm5bYGShcCg0PN8qFxa"
    
    # JWT配置
    SECRET_KEY: str = os.getenv("SECRET_KEY", "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7")
    ACCESS_TOKEN_EXPIRE_MINUTES: int = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", 60 * 24))  # 1天
    
    # MinIO配置
    MINIO_ENDPOINT: str ="http://172.19.177.2:9000/"
    MINIO_ACCESS_KEY: str = "minioadmin"
    MINIO_SECRET_KEY: str = "minioadmin"
    MINIO_BUCKET_NAME: str = "mybucket"
    MINIO_SECURE: bool = False


@lru_cache
def get_settings():
    """ 缓存配置 """
    return Settings()


settings = Settings()